package com.yellowpage.bigdata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.LinkedMap;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.yellowpage.white.common.Constants;
import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;
import com.yellowpage.bigdata.data.TaO2oChlDay;
import com.yellowpage.bigdata.dao.TaO2oChlDayDao;

public class TaO2oChlDayServiceImpl extends BaseServiceImpl implements TaO2oChlDayService {
	  private TaO2oChlDayDao taO2oChlDayDao;
	
	  public void setTaO2oChlDayDao(TaO2oChlDayDao taO2oChlDayDao) {
		  this.taO2oChlDayDao = taO2oChlDayDao;
	  }
	
	
	  /* Smooth generate Method */ 
	  public TaO2oChlDay getTaO2oChlDay(Long id){
	      return taO2oChlDayDao.get(id);
	  }
	  
    public List<TaO2oChlDay> getAllTaO2oChlDay(){
        return taO2oChlDayDao.getAll();
    }
    
    public List<Map> queryTaO2oChlDayByPage(Map queryMap, PageRender pageRender) {		
        
    	QueryAssistantSQL sqlAssistant = baseDao.newQueryAssistantSQL(
    			
						"SELECT A.DAY_ID,A.O2O_ID,B.`NAME` AS O2O_NAME,A.CHANNEL_NO,C.`NAME` AS CHANNEL_NAME,A.YES_UV,A.UV,A.YES_PV,A.PV,A.TOTAL_UV,A.TOTAL_PV"
								+ " FROM TA_O2O_CHL_DAY A "
								+ " JOIN O2O_CODE B ON B.ID = A.O2O_ID"
								+ " JOIN OMC_DIC_TABLE C ON C.VALUE = A.CHANNEL_NO AND C.DIC_NAME = 'O2O_LOG_CHANNEL_NO' WHERE 1=1"
								)
				.addCondition(" and A.DAY_ID >= :dayId ")
				.addCondition(" and A.DAY_ID <= :etlTime ")
				.addCondition(" and A.O2O_ID = :o2oId ")
				.addCondition(" and A.CHANNEL_NO = :channelNo ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}
    
	@Override
	public List<Map> queryTaO2oChlDayByPage(Map queryMap) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(

						"SELECT A.DAY_ID,A.O2O_ID,B.`NAME` AS O2O_NAME,A.CHANNEL_NO,C.`NAME` AS CHANNEL_NAME,A.YES_UV,A.UV,A.YES_PV,A.PV,A.TOTAL_UV,A.TOTAL_PV"
								+ " FROM TA_O2O_CHL_DAY A "
								+ " JOIN O2O_CODE B ON B.ID = A.O2O_ID"
								+ " JOIN OMC_DIC_TABLE C ON C.VALUE = A.CHANNEL_NO WHERE 1=1")
				.addCondition(" and A.DAY_ID >= :dayId ")
				.addCondition(" and A.DAY_ID <= :etlTime ")
				.addCondition(" and A.O2O_ID = :o2oId ")
				.addCondition(" and A.CHANNEL_NO = :channelNo ");

		List<Map> resultList = sqlAssistant.query(
				baseDao.getConnection(), queryMap);

return resultList;
	}
	  
    public void saveTaO2oChlDay(TaO2oChlDay taO2oChlDay){
        taO2oChlDayDao.save(taO2oChlDay);
    }
    
	  public void updateTaO2oChlDay(TaO2oChlDay taO2oChlDay){
	      taO2oChlDayDao.update(taO2oChlDay);
	  }
	  
	  public void delete(List deleteList){
	      taO2oChlDayDao.remove(deleteList);
	  }




	@Override
	public Map getTaO2oIdMap() {
		Map dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.name as NAME, a.ID as VALUE  FROM O2O_CODE a  ");
		sqlAssistant.setTail("order by (a.ID+0)");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("VALUE") + "-" + resultList.get(i).get("NAME"));
		}
		return dataSourceMap;
	}


	@Override
	public Map getTaO2oChannelMap() {
		Map dataSourceMap = new LinkedMap();
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(" SELECT a.name as NAME, a.VALUE as VALUE  FROM OMC_DIC_TABLE a where dic_name='O2O_LOG_CHANNEL_NO' ");
		sqlAssistant.setTail("order by (a.VALUE+0)");
		List<Map> resultList = sqlAssistant.query(baseDao.getConnection(),
				new HashMap());
		for (int i = 0; i < resultList.size(); i++) {
			dataSourceMap.put(resultList.get(i).get("VALUE"), resultList.get(i)
					.get("VALUE") + "-" + resultList.get(i).get("NAME"));
		}
		return dataSourceMap;
	}


	
	  
	 
	
}	